package edu.washington.vaccine.ui;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.washington.vaccine.R;
import edu.washington.vaccine.backend.Patient;
import edu.washington.vaccine.backend.VaccineDB;
import edu.washington.vaccine.backend.VaccineLocalHelper;

/*
 * 
 * post-choose vaccine screen, double checks with user
 * that the changes (highlighted in blue) are the vaccines
 * the user wishes to add to the current patient's data
 */

public class PostChooseVaccineActivity extends Activity {
	private VaccineLocalHelper localDB;
	private VaccineDB exDB;
	Patient p;

	//depends on a serialized String[] containing an array of vaccine id's
	//depends on a serialized String containing vaccine names and dates
	//depends on a serialized Patient p to populate form with other patient data
	//depends on save_patient_data.xml
	//
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		@SuppressWarnings("unused")
		final Activity thisActivity = this;
		setContentView(R.layout.save_patient_data);
		View root = ((ViewGroup) findViewById(android.R.id.content)).getRootView();
		root.setFocusable(true);
		root.setFocusableInTouchMode(true);
		root.requestFocus();
		Bundle data = getIntent().getExtras();
		exDB = VaccineDB.getInstance();
		localDB = VaccineLocalHelper.getInstance(this);

		final String printInfo = (String) data.getSerializable("printInfo");
		final String[] newVaccines = (String[]) data.getSerializable("newVaccine");

		p = (Patient) data.getSerializable("patient");


		TextView box = (TextView) findViewById(R.id.textView3);
		box.setText("" + p.id);

		box = (TextView) findViewById(R.id.textView5);
		box.setText(p.fname + " " + p.lname);

		box = (TextView) findViewById(R.id.textView7);
		box.setText(p.dob);

		box = (TextView) findViewById(R.id.textView9);
		box.setText("" + p.location);

		box = (TextView) findViewById(R.id.textView11);
		box.setText(p.mother);

		box = (TextView) findViewById(R.id.textView13);
		box.setText(p.father);

		box = (TextView) findViewById(R.id.textView15);
//		String a = "";
//		for(String s : newVaccines) {
//			a += s;
//		}
		box.setText(printInfo.trim());

		box = (TextView) findViewById(R.id.textView16);
		VaccineDB db = VaccineDB.getInstance();
		VaccineLocalHelper helper = VaccineLocalHelper.getInstance(this);
		StringBuilder sb = new StringBuilder();
		if (db.isOnlineMode()) {
			Map<String, String> findVacc = new HashMap<String, String>();
			Set<String[]> vaccines = db.getPatientsVaccines("" + p.id);
			Cursor c = helper.getReadableDatabase().rawQuery("select vid, vname from Vaccines", new String[]{});
			while (c.moveToNext()) {
				findVacc.put("" + c.getInt(0), c.getString(1));
			}

			if (vaccines != null) {
				for (String[] s : vaccines) {
					String vname = findVacc.get(s[0]);
					sb.append(vname + " (Dose " + s[2] + "), " + s[1] + "\n");
				}


				box.setText(sb.toString());				
			}

		} else {
			box.setText(helper.getPatientsVaccines("" + p.id));
		}

		Button editButton = (Button) findViewById(R.id.save_button);
		//Upon clicking the save_button, the highlighted blue selected vaccines
		//will be uploading to either the online or offline db depending
		//on the application's mode
		editButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				for(int i = 1; i < newVaccines.length; i++) {
					if(newVaccines[i] != null) {
						if (exDB.isOnlineMode()) {
							exDB.addVaccine(newVaccines[0], newVaccines[i]);
						} else {
							localDB.addVaccine(newVaccines[0], newVaccines[i]);
						}
					}
				}

//				Intent i = new Intent(thisActivity, VaccineActivity.class);
//				VaccineActivity parentActivity = (VaccineActivity)thisActivity.getParent();
//				parentActivity.replaceContentView("nextactivity", i);
//				FirstGroup.group.addChildActivity("nextactivity", i);
				FirstGroup.group.home();
			}
		});
	}
}
